import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys
import subprocess
import socket


#选择浏览器
def choose_browser(browser='Firefox'):
    if browser=='Firefox':
        
        #到这里下载最新火狐浏览器"https://www.mozilla.org/zh-CN/firefox/all/desktop-release/win64-msi/zh-CN/"
        #下面是firefox接管已打开网页的方法，调试时使用
        
        #"C:\Program Files\Mozilla Firefox\firefox.exe" -marionette -start-debugger-server 2828
        command = '"C:\\Program Files\\Mozilla Firefox\\firefox.exe"'
        command1=command+' -marionette -start-debugger-server 2828'
        if not test_port('127.0.0.1',2828):
            
            subprocess.Popen(command1)


        #firefox_services = Service(executable_path='C:/py/0929/geckodriver.exe',port=3000,service_args=['--marionette-port', '2828', '--connect-existing'])
        firefox_services = Service(service_args=['--marionette-port', '2828', '--connect-existing'])
        #driver = webdriver.Firefox(service=firefox_services)
        #driver.close()
        
        return webdriver.Firefox(service=firefox_services)
    if browser=='Chrome':
        # 创建 Options 选项对象
        opt = Options()
        opt.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'	# 指定 Chrome 浏览器的路径

        # 创建 Service 对象
        ser = Service()	
        ser.executable_path = r'C:\Program Files\Python312\chromedriver.exe'	# 指定 ChromeDriver 的路径
        chrome_location=r"C:\Program Files\Google\Chrome\Application\chrome.exe"
        '''
        
##        #open_chrome_command=chrome_location+' --remote-debugging-port=9222 --user-data-dir="c:\temp"'
##
##        open_chrome_command=chrome_location+' --remote-debugging-port=9222'
##        print(open_chrome_command)
##        command = '"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"'
##        command1=command+' --remote-debugging-port=9222'
##        print(command1)
##        print(command)
##
##        # 运行命令
##        #os.system(command1)
##        subprocess.Popen(command1)
        '''
        return webdriver.Chrome(options=opt)

#选中元素的xpath并点击
def click1(xpath,comments=''):
    wait0(xpath)
    print(xpath,'click',comments)
    element = driver.find_element(By.XPATH, xpath)
    element.click()
    time.sleep(1)
#选中输入框的xpath并输入text    
def input1(xpath,text):
    wait0(xpath)
    print(xpath,text)
    element = driver.find_element(By.XPATH, xpath)
    element.send_keys(text)
    time.sleep(1)

#等待元素出现
def wait0(xpath,comments=''):
    print(xpath,comments)

    try:
        element = driver.find_element(By.XPATH, xpath)
        print(xpath,'已出现')
    except:
        print('waiting ......')
        time.sleep(5)
        
        wait0(xpath)
        
#打开网页        
def open(link,comments=''):
    print(comments,link)
    driver.get(link)
    time.sleep(2)

#获取xpath的文字
def get_text(xpath):
    #xpath='//*[@id="main_price"]/em'
    element = driver.find_element(By.XPATH, xpath)
    print('价格是',element.text)
    return element.text

#测试端口是否通
def test_port(ip, port):
    try:
        socket.create_connection((ip, port), timeout=1).close()
        print(ip,port,'端口通')
        return True
    except socket.error as e:
        print(f"Port {port} is not available.")
        return False

        
if __name__ == "__main__":
    print('start')


    #选择浏览器并打开，默认是火狐
    driver = choose_browser()


    
    #open('https://plogin.m.jd.com/login/login?')
    open('https://item.m.jd.com/product/10086891405101.html')


    #input1('//*[@id="kw"]','hello, world')
    driver.close()
    #driver.quit()
    #wait0('//*[@id="msShortcutMenu"]/span')
    #open('https://item.m.jd.com/product/10086891405101.html')
    #get_text('//*[@id="main_price"]/em')


